/*----------------------------------------------------*
       Project : Covid 19
       Purpose : Processing data for regressions
       updated : June 17 2020    17:23 IST
*-----------------------------------------------------*/

** SETTING UP
version 15
clear all
pause on
set more off
qui cap log c


loc path_LM = "/Users/louis-maeljean/Dropbox (MIT)/West Bengal Information Campaign/AER_I/for_submission"
loc path = "`path_LM'" 		//other users should change this

cd "`path'"

use "`path'/data/outcomes_asha_reg_input3unp.dta",clear 

tab treatment, gen(T)

rename T1 T0    //Control
rename T2 T1    //Treatment1
rename T3 T2    //Treatment2
rename T4 T3	//Treatment3
rename T5 T4	//Treatment4
rename T6 T5	//Treatment5
rename T7 T6	//Treatment6
rename T8 T7	//Treatment7
rename T9 T8	//Treatment8


gen Treatment = 1    //generating a variable for all the videos
replace Treatment = 0 if T0 == 1
label var Treatment "TREATMENT"

* Labellig each treatment with the video content *

label var T1 "Hyg + Ext + Type1"        //Video1
label var T2 "SD + Ext + Type1"         //Video2
label var T3 "Hyg + No_Ext + Type1"     //Video3
label var T4 "SD + No_Ext + Type1"      //Video4
label var T5 "Hyg + Ext + Type2"        //Video5
label var T6 "Hyg + No_Ext + Type2"     //Video6
label var T7 "SD + Ext + Type2"         //Video7
label var T8 "SD + No_Ext + Type2"      //Video8

gen phase = 0
replace phase = 1 if id_date >= td(07may2020)
replace phase = 2 if id_date >= td(11may2020)
replace phase = 3 if id_date >= td(20may2020)
replace phase = 4 if id_date >= td(01aug2020)
replace phase = 5 if id_date >= td(01sep2020)

egen enter_phase = min(phase), by(unique_id)

label var phase "Survey phases"
label var enter_phase "Phase in which asha entered the survey"

sort unique_id id_date, stable
cap drop interview_round totalrounds
by unique_id: gen interview_round = _n
sort unique_id interview_round, stable
by unique_id: gen totalrounds = _N

// #### Construct symptom variables ### 
egen respiratory_count = rowtotal( cough breathing )

replace asha_symptoms_1 = 0 if missing(asha_symptoms_1)
replace asha_symptoms_2 = 0 if missing(asha_symptoms_2)
replace asha_symptoms_4 = 0 if missing(asha_symptoms_4)

gen flw_fever = asha_symptoms_1
egen flw_respiratory_count = rowtotal(asha_symptoms_2 asha_symptoms_4)

gen total_symptoms_fr = fever + respiratory_count
gen any_symptoms_fr = fever > 0 | respiratory_count > 0


label var respiratory_count "Cough + Breathing"
label var total_symptoms_fr "Fever + Respiratory_count"
label var any_symptoms_fr "Binary variable for fever > 0 | respiratory_count > 0 "

replace whatsapp_yn = 0 if smartphone_yn == 0
gen yearsofschooling = .
replace yearsofschooling = 0 if flw_edu == 1
replace yearsofschooling = 7 if flw_edu == 2
replace yearsofschooling = 11 if flw_edu == 3
replace yearsofschooling = 12 if flw_edu == 4
replace yearsofschooling = 14 if flw_edu == 5
replace yearsofschooling = 17 if flw_edu == 6
replace yearsofschooling = 19 if flw_edu == 7

gen nophone = 0
replace nophone = 1 if smartphone_yn == 0
gen famphone = 0
replace famphone = 1 if smartphone_yn == 2
gen ownphone = 0
replace ownphone = 1 if smartphone_yn == 1

lab var nophone "No Access to Smartphone"
lab var famphone "Access to Smartphone through Family Member"
lab var ownphone "Own Smartphone"


lab var flw_households "Households Under ASHA"
lab var smartphone_yn "Smartphone Access"
lab var whatsapp_yn "Whatsapp Access"
lab var yearsofschooling "Years of Schooling"


bysort unique_id: gen exit_phase =  cond(exit_flag==1, phase[_n], .)	//generate ASHA exit_phase (missing if never exited)
bysort unique_id (exit_phase): replace exit_phase = exit_phase[1]

bysort unique_id: egen total_rounds = max(interview_round) 	//total number of times each ASHA was interviwed

gen flagphase0 = 1 if id_date < td(07may2020) && interview_round == 1
replace flagphase0 = 0 if flagphase0 == .

egen phase0 = total(flagphase0), by(unique_id)
gen flagphase1 = 1 if id_date > td(06may2020) && id_date < td(11may2020) && interview_round == 1
replace flagphase1 = 0 if flagphase1 == .
egen phase1 = total(flagphase1), by(unique_id)

gen TreatXphase1 = Treatment*(phase==1) //Generate interacted Treatment X phase coefficients
gen TreatXphase2 = Treatment*(phase==2)
gen TreatXphase3 = Treatment*(phase==3)
gen TreatXphase4 = Treatment*(phase==4)
gen TreatXphase5 = Treatment*(phase==5)

label var TreatXphase1  "Phase 1 Treatment Effect"
label var TreatXphase2  "Phase 2 Treatment Effect"
label var TreatXphase3  "Phase 3 Treatment Effect"
label var TreatXphase4  "Phase 4 Treatment Effect"
label var TreatXphase5  "Phase 5 Treatment Effect"

lab var phase0 "Probability of being sampled in 21st April to 06th May" 
lab var phase1 "Probability of being sampled in 7th May to 10th May"

winsor flw_households, generate(Wflw_households) p(0.01) highonly

save "`path'/data/outcomes_asha_reg_input3.dta",replace



************************************=========*********************************
************************************=========*********************************
* END
************************************=========*********************************
************************************=========*********************************


